<?php
/**
 * Create By: PhpStorm
 * User: niechenguang
 * File: Circle.php
 * Date: 2017/1/2
 * Time: 23:13
 */

namespace app\api\model\page\community;

use app\api\model\dao\Ad;
use app\api\model\dao\Banner;
use app\api\model\dao\BannerImage;
use app\api\model\dao\Circle;
use app\api\model\dao\Note;
use app\api\model\dao\NotePraise;
use app\api\model\dao\NoteReply;
use app\api\model\dao\UserAccount;
use app\api\model\data\note\NoteData;
use app\library\Flag;
use app\library\PageBase;
use think\Exception;
use think\Log;

class Index extends PageBase
{

    private $objCircle;
    private $objNote;
    private $objUserAccount;
    private $objNotePraise;
    private $objNoteReply;
    private $objNoteData;
    private $objBanner;
    private $objBannerImage;
    private $objAd;

    public function __construct()
    {
        parent::__construct();

        $this->objCircle = new Circle();
        $this->objNote = new Note();
        $this->objUserAccount = new UserAccount();
        $this->objNotePraise = new NotePraise();
        $this->objNoteReply = new NoteReply();
        $this->objNoteData = new NoteData();
        $this->objBanner = new Banner();
        $this->objBannerImage = new BannerImage();
        $this->objAd = new Ad();
    }

    public function myExcute($param)
    {
        $this->checkToken($param);

        $this->user_id = !empty($this->user_id) ? $this->user_id : 0;

        // 1、获取banner图
        $this->data['banner'] = $this->getBanner(17);

        // 2、获取广告图
//        $ad = $this->getBanner(18);
//        if (!empty($ad)) {
//            $this->data['ad'] = $ad[0];
//        }
        $this->data['ad'] = $this->getBanner(18);

        // 3、获取帖子列表
        $arrNote = $this->getNoteList($this->offset, $this->limit);

        foreach ($arrNote as &$note) {
            $note['is_praise'] = 0;
            $conds = [];
            $conds['note_id'] = ['=', $note['note_id']];
            $conds['userid'] = ['=', $this->user_id];
            $retCnt = $this->objNotePraise->getNotePraiseCountByConds($conds);
            if (!empty($retCnt)) {
                $note['is_praise'] = 1;
            }
        }
        unset($note);

        $this->data['note'] = $arrNote;

    }

    /**
     * 获取banner位图
     *
     * @return array
     */
    public function getBanner($position)
    {
        $arrAd = [];
        $ad = $this->objAd->getAdByPid($position);
        if (!empty($ad)) {
            foreach ($ad as $val) {
                if (0 != $val['status']) {
                    continue;
                }
                $arrAd[] = [
                    'title' => $val['ad_name'],
                    'link' => $val['ad_link'],
                    'pic' => $val['ad_code'],
                ];
            }
        }
        return $arrAd;
    }

    /**
     * 获取帖子列表
     *
     * @return array
     */
    public function getNoteList($offset, $limit)
    {
        return $this->objNoteData->newNote(0, $offset, $limit);
    }
}
